package mortal.game.control
{
    import Framework.MQ.*;
    import Message.Command.*;
    import Message.Game.*;
    import com.gengine.global.*;
    import com.gengine.utils.*;
    import com.mui.controls.*;
    import modules.*;
    import mortal.common.global.*;
    import mortal.component.gconst.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.*;
    import mortal.game.scene.ai.*;
    import mortal.game.view.delegateTask.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class KillEvilTaskController extends Controller
    {
        private var _killEvilTaskModule:KillEvilTaskModule;

        public function KillEvilTaskController()
        {
            
        }

        private function onOpenKillEvilTaskWin(event:DataEvent) : void
        {
            if (cache.role.roleInfo.level < 35)
            {
                MsgManager.addBroadCast("等级需要达到35级才能发布诛邪令任务，赶紧加把劲升级哦。");
                
            }
            view.show();
            
        }

        private function onKillEvilTaskPropUse(event:DataEvent) : void
        {
            var e:* = event;
            if (cache.role.roleInfo.level < 35)
            {
                MsgManager.addBroadCast("等级需要达到35级才能发布诛邪令任务，赶紧加把劲升级哦。");
                
            }
            var prop:* = e.data as ItemData;
            var htmltext:* = HTMLUtil.addColor(prop.itemInfo.item.name, ColorConfig.instance.getItemColor(prop.itemInfo.item.color).color) + "可在" + HTMLUtil.addColor("诛邪仙尊", "#00FF00") + "处发布诛邪任务，是否要前往。" + HTMLUtil.addColor("（每天最多可以发布五次）", "#00FF00");
            Alert.okLabel = "自动前往";
            Alert.buttonWidth = 75;
            Alert.show(htmltext, "提示", Alert.OK | Alert.CANCEL, null, function (param1:int) : void
            {
                if (param1 == Alert.OK)
                {
                    AIManager.onAutoPathAIControl(cache.scene.getEvilTaskNpc());
                }
                
            }
            );
            
        }

        public function onPublishKillEvilTask(event:DataEvent) : void
        {
            var type:int;
            var htmlText:String;
            var startH:String;
            var endH:String;
            var e:* = event;
            type = e.data as int;
            if ((ClockManager.instance.nowDate.hours < GameConst.ReleaseKillEvilTaskTime_start || ClockManager.instance.nowDate.hours >= GameConst.ReleaseKillEvilTaskTime_end) && !Global.isDebugModle)
            {
                startH = GameConst.ReleaseKillEvilTaskTime_start < 10 ? ("0" + GameConst.ReleaseKillEvilTaskTime_start) : ("" + GameConst.ReleaseKillEvilTaskTime_start);
                endH = GameConst.ReleaseKillEvilTaskTime_end < 10 ? ("0" + GameConst.ReleaseKillEvilTaskTime_end) : ("" + GameConst.ReleaseKillEvilTaskTime_end);
                MsgManager.addBroadCast("诛邪令发布时间：" + startH + "：00 - " + endH + "：00");
                
            }
            else
            {
                if (cache.killEvilTask.releaseInfo.canReleaseCount == 0)
                {
                    MsgManager.addBroadCast("您今天已经发布满次数了，明天还可继续发布！");
                    
                }
                if (cache.killEvilTask.getOwnNumByType(type) == 0)
                {
                    MsgManager.addBroadCast("您没有该令牌，可以到商场购买或者击杀副本boss获得。");
                    
                }
            }
            var color:* = ColorConfig.instance.getItemColor(cache.killEvilTask.getKillEvilTaskColor(type));
            htmlText = HTMLUtil.addColor("您现在选择发布的是：", GlobalStyle.color1) + HTMLUtil.addColor(cache.killEvilTask.getKillEvilTaskName(type), color.color) + HTMLUtil.addColor("\n（被领取后可获得：", GlobalStyle.color1) + HTMLUtil.addColor(cache.killEvilTask.getExpByType(type) + "经验", GlobalStyle.color7) + HTMLUtil.addColor("）\n", GlobalStyle.color1) + HTMLUtil.addColor("您今日还可发布" + cache.killEvilTask.releaseInfo.canReleaseCount + "次", GlobalStyle.color6);
            Alert.buttonWidth = 75;
            Alert.okLabel = "确定发布";
            Alert.calcelLabel = "取消发布";
            Alert.show(htmlText, "诛邪令", Alert.OK | Alert.CANCEL, null, function (param1:int) : void
            {
                if (param1 == Alert.OK)
                {
                    GameProxy.killEvilTask.releaseKillEvilTask(type);
                }
                
            }
            );
            
        }

        public function onGetKillEvilTask(event:DataEvent) : void
        {
            var type:int;
            var startH:String;
            var endH:String;
            var e:* = event;
            type = e.data as int;
            if ((ClockManager.instance.nowDate.hours < GameConst.GetKillEvilTaskTime_start || ClockManager.instance.nowDate.hours >= GameConst.GetKillEvilTaskTime_end) && !Global.isDebugModle)
            {
                startH = GameConst.GetKillEvilTaskTime_start < 10 ? ("0" + GameConst.GetKillEvilTaskTime_start) : ("" + GameConst.GetKillEvilTaskTime_start);
                endH = GameConst.GetKillEvilTaskTime_end < 10 ? ("0" + GameConst.GetKillEvilTaskTime_end) : ("" + GameConst.GetKillEvilTaskTime_end);
                MsgManager.addBroadCast("诛邪令领取时间：" + startH + "：00 - " + endH + "：00");
                
            }
            else
            {
                if (cache.killEvilTask.myGetInfo.executeKillEvilType != 0)
                {
                    MsgManager.addBroadCast("您已经有诛邪任务在身，请完成后再来领取。");
                    
                }
                if (cache.killEvilTask.myGetInfo.canGetCount == 0)
                {
                    MsgManager.addBroadCast("您已完成今日所有次数，明天还能再次领取。");
                    
                }
                if (cache.killEvilTask.getReaseNumByType(type) == 0)
                {
                    MsgManager.addBroadCast("没有此类型的诛邪令任务，请选择其它诛邪任务或刷新重试！");
                    
                }
            }
            var color:* = ColorConfig.instance.getItemColor(cache.killEvilTask.getKillEvilTaskColor(type));
            var htmlText:* = HTMLUtil.addColor("您现在要领取的是：", GlobalStyle.color1) + HTMLUtil.addColor(cache.killEvilTask.getKillEvilTaskName(type), color.color) + HTMLUtil.addColor("\n（完成后可获得：", GlobalStyle.color1) + HTMLUtil.addColor(cache.killEvilTask.getExpByType(type) + "经验", GlobalStyle.color7) + HTMLUtil.addColor("）\n注：\n        必须在2个小时内完成，超过时间则失败，不获得任何经验。", GlobalStyle.color1) + HTMLUtil.addColor("\n您今日还可领取" + cache.killEvilTask.myGetInfo.canGetCount + "次", GlobalStyle.color6);
            Alert.buttonWidth = 75;
            Alert.okLabel = "确定领取";
            Alert.calcelLabel = "暂不领取";
            Alert.show(htmlText, "诛邪令", Alert.OK | Alert.CANCEL, null, function (param1:int) : void
            {
                if (param1 == Alert.OK)
                {
                    GameProxy.killEvilTask.getKillEvilTask(type);
                }
                
            }
            );
            
        }

        public function onChangeCurrentPanel(event:DataEvent) : void
        {
            this._killEvilTaskModule.setCurrentPanel(event.data as int);
            
        }

        private function onHintIconClick(event:DataEvent) : void
        {
            var _loc_2:* = event.data as SSystemGetTaskIssue;
            var _loc_3:* = ColorConfig.instance.getItemColor(cache.killEvilTask.getKillEvilTaskColor(_loc_2.issueItemColor));
            var _loc_4:* = HTMLUtil.addColor("您发布的", GlobalStyle.color1) + HTMLUtil.addColor(cache.killEvilTask.getKillEvilTaskName(_loc_2.issueItemColor), _loc_3.color) + HTMLUtil.addColor("任务已经被领取，获得", GlobalStyle.color1) + HTMLUtil.addColor(_loc_2.issueExp + "经验。", GlobalStyle.color7);
            Alert.show(_loc_4, "诛邪令");
            
        }

        private function onReceiveReleaseInfo(param1:MessageBlock) : void
        {
            var _loc_2:* = param1.messageBase as SKillEvilReleaseInfo;
            cache.killEvilTask.releaseInfo = _loc_2;
            if (this._killEvilTaskModule)
            {
                this._killEvilTaskModule.updateReleaseInfo();
                this._killEvilTaskModule.updateCanGetInfo();
            }
            
        }

        private function onReceiveCanGetInfo(param1:MessageBlock) : void
        {
            var _loc_2:* = param1.messageBase as SKillEvilCanGetInfo;
            cache.killEvilTask.canGetInfo = _loc_2;
            if (this._killEvilTaskModule)
            {
                this._killEvilTaskModule.updateCanGetInfo();
            }
            
        }

        private function onReceiveMyKillInfo(param1:MessageBlock) : void
        {
            var _loc_2:* = param1.messageBase as SMyKillEvilGetInfo;
            cache.killEvilTask.myGetInfo = _loc_2;
            if (this._killEvilTaskModule)
            {
                this._killEvilTaskModule.updateHadGetInfo();
            }
            
        }

        private function onKillTaskIssue(param1:MessageBlock) : void
        {
            var _loc_2:* = param1.messageBase as SSystemGetTaskIssue;
            var _loc_3:* = new KillEvilTaskHintIcon(_loc_2);
            _loc_3.show();
            
        }

        private function serverBagDataChangehandler(param1:Object = null) : void
        {
            if (this._killEvilTaskModule)
            {
                this._killEvilTaskModule.updateReleaseInfo();
            }
            
        }

        private function onCompleteKillEvilTask(param1:MessageBlock) : void
        {
            
        }

        override protected function initView() : IView
        {
            if (!this._killEvilTaskModule)
            {
                this._killEvilTaskModule = new KillEvilTaskModule();
            }
            return this._killEvilTaskModule;
        }

        override protected function initServer() : void
        {
            Dispatcher.addEventListener(EventName.KillEvilTaskOpenTaskWin, this.onOpenKillEvilTaskWin);
            Dispatcher.addEventListener(EventName.KillEvilTaskUsePropFromPack, this.onKillEvilTaskPropUse);
            Dispatcher.addEventListener(EventName.KillEvilTaskPublishTask, this.onPublishKillEvilTask);
            Dispatcher.addEventListener(EventName.KillEvilTaskGetTask, this.onGetKillEvilTask);
            Dispatcher.addEventListener(EventName.KillEvilTaskChangeCurrentPanel, this.onChangeCurrentPanel);
            Dispatcher.addEventListener(KillEvilTaskHintIcon.KillEvilHintIconClick, this.onHintIconClick);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGateKillEvilReleaseInfo, this.onReceiveReleaseInfo);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGateKillEvilCanGetInfo, this.onReceiveCanGetInfo);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGateMyKillEvilGetInfo, this.onReceiveMyKillInfo);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGateSystemGetTaskIssue, this.onKillTaskIssue);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGateCompleteKillEvilTask, this.onCompleteKillEvilTask);
            NetDispatcher.addCmdListener(ServerCommand.BackpackDataChange, this.serverBagDataChangehandler);
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsChange, this.serverBagDataChangehandler);
            
        }

    }
}
